package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.dto.MakeTagDTO;
import com.yuanfeng.userms.dto.TagInfoPageDTO;
import com.yuanfeng.userms.entity.UserTagInfoEntity;
import com.yuanfeng.userms.mapper.UserTagInfoMapper;
import com.yuanfeng.userms.service.UserTagInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Service
public class UserTagInfoServiceImpl extends ServiceImpl<UserTagInfoMapper, UserTagInfoEntity>
    implements UserTagInfoService{

    @Override
    public ResponseResult deleteTagInfo(TagInfoPageDTO dto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(dto.getToken());
        if (null != userInfo) {
            this.baseMapper.update(null,new LambdaUpdateWrapper<UserTagInfoEntity>()
                            .set(UserTagInfoEntity::getTagIsactive,00)
                            .set(UserTagInfoEntity::getTagUpdData,DateUtils.getNowDateToString())
                            .eq(UserTagInfoEntity::getTagId,dto.getTagId()));
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult updateTagInfo(TagInfoPageDTO dto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(dto.getToken());
        if (null != userInfo) {
            Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<UserTagInfoEntity>()
                            .eq(UserTagInfoEntity::getTagName,dto.getTagName())
                            .ne(UserTagInfoEntity::getTagId,dto.getTagId()));
            if (count > 0) {
                return ResponseResult.fail(BizCodeEnume.ADD_TAG_INFO_ERROR);
            }
            //设置日期格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dto.setTagUpdData(DateUtils.getNowDateToString());
            this.baseMapper.updateTagInfo(dto);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult addTagInfo(TagInfoPageDTO dto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(dto.getToken());
        if (null != userInfo) {
            //判断标签名是否存在
            Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<UserTagInfoEntity>()
                            .eq(UserTagInfoEntity::getTagName,dto.getTagName()));
            if (count > 0) {
                return ResponseResult.fail(BizCodeEnume.ADD_TAG_INFO_ERROR);
            }
            this.baseMapper.addTagInfo(dto);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult makeUserTag(MakeTagDTO dto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(dto.getToken());
        if (null != userInfo) {
            String[] userIdArray = dto.getUserIdArray();
            //遍历传过来的userid数组判断是否有标签
            for (String userid : userIdArray) {
                String str = StringUtils.join(dto.getTagIdArray(), ",");
                Map<String, Object> paraMap = new HashMap<>();
                paraMap.put("memberId",userid);
                paraMap.put("tagId",str);
                this.baseMapper.updateTag(paraMap);
            }
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryTagInfo(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {
            //分页查询数据
            Page<UserTagInfoEntity> page = new Page<>((Integer) map.get("page"), (Integer) map.get("limit"));
            IPage<UserTagInfoEntity> iPage = this.baseMapper.selectPage(page, new LambdaQueryWrapper<UserTagInfoEntity>()
                    .eq(UserTagInfoEntity::getTagIsactive, 1)
                    .like(map.get("tagName") != null && !"".equals(map.get("tagName")), UserTagInfoEntity::getTagName, map.get("tagName"))
                    .orderByAsc(UserTagInfoEntity::getTagWeight));
            return ResponseResult.success(PageUtils.getPage(iPage));
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




